package com.example.designdemo.singleton;

import java.util.HashSet;
import java.util.Set;

public class SingletonTest {


    public static void main(String[] args) {
        /*Singleton1 singleton1 = Singleton1.getSingleton();
        Singleton1 singleton2 = Singleton1.getSingleton();
        System.out.println(singleton1==singleton2);


        Singleton2 singleton3 = Singleton2.getSingleton();
        Singleton2 singleton4 = Singleton2.getSingleton();
        System.out.println(singleton3==singleton4);*/

        Set<Singleton1> set1 = new HashSet<>();
        Set<Singleton2> set2 = new HashSet<>();

        for (int i=0;i<20;i++){
            new Thread(){
                @Override
                public void run() {
                    Singleton1 singleton1 = Singleton1.getSingleton();
                    set1.add(singleton1);

                    Singleton2 singleton2 = Singleton2.getSingleton();
                    set2.add(singleton2);
                }
            }.start();
        }

        while (Thread.activeCount()>1)
            Thread.yield();

        System.out.println("单例一的个数："+set1.size()+"单例二的个数："+set2.size());
    }
}
